<?php
// 1908080121李定邦、1902030115李龙烽
namespace app\admin\controller;

use app\BaseController;
use think\App;

class ForumRec extends BaseController {
    private $_mes;

    public function __construct(App $app) {
        parent::__construct($app);
        $this->checkIsAdminLogin();
        $this->_mes = app('db')->name('mes');
    }

    /**
     * 帖子列表
     * @return mixed
     */
    public function index() {
        $keyword = $this->request->get('keyword');
        $query   = $this->_mes->where("m_title", "LIKE", "%$keyword%")->where("is_del", 1);
        $data    = $query->paginate(10);
        $page    = $data->render();
        $data = [
            'list'     => $data,
            'page'     => $page,
        ];

        return app('view')->fetch('', $data);
    }

    /**
     * 添加
     * @return void
     */
    public function add() {
        if ($this->request->isPost()) {
            $m_title   = $this->request->post('m_title', '', 'trim');
            $m_content = $this->request->post('m_content', '', 'trim');
            $m_sid     = $this->request->post('m_sid', '', 'trim');
            $munick    = app('db')->name('admin_user')->where('id', session('login_admin_id'))->value('username');
            $data = [
                'm_title'   => $m_title,
                'm_content' => $m_content,
                'm_sid'     => $m_sid,
                'is_del'    => 1,
                'm_unick'   => $munick,
            ];
            $res = app('db')->name('mes')->insert($data);
            $res ? $this->success('添加成功', '/forum/rec') : $this->error('添加失败');
        } else {
            $sec_list = $this->listSec();
            $data = [
                'sec_list' => $sec_list,
            ];
            return app('view')->fetch('', $data);
        }
    }

    /**
     * 删除帖子
     * @return \think\response\Json
     */
    public function delete() {
        $mid = $this->request->get('mid');
        $mid = is_array($mid) ? $mid : [$mid];
        $res = $this->_mes->whereIn('m_id', $mid)->delete();
        $data = [
            'rs' => true,
            'msg' => $res ? '彻底删除成功' : '未知错误'
        ];

        return json($data);
    }

    /**
     * 恢复帖子
     * @return \think\response\Json
     */
    public function normal() {
        $mid = $this->request->get('mid');
        $mid = is_array($mid) ? $mid : [$mid];
        $res = $this->_mes->whereIn('m_id', $mid)->update(['is_del' => 0]);
        $data = [
            'rs' => true,
            'msg' => $res ? '恢复成功' : '未知错误'
        ];

        return json($data);
    }
}